/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.gateway.dianlan.bits;

import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.component.gateway.core.util.HexUtil;
import info.peigen.hotpot.component.gateway.dianlan.bits.base.BitParserBase;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanWaterCoolingUnitBmsRequestMode;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanWaterCoolingUnitBmsThermalManagementRelayStatus;
import info.peigen.hotpot.component.gateway.dianlan.facade.enums.DianlanWaterCoolingUnitChargingStatus;
import info.peigen.hotpot.component.gateway.dianlan.protocol.passive.DianlanWaterCoolingUnitControlDataGram;

/**
 * <b>(DianlanWaterCoolingUnitControlDataGramReasonBits)</b>
 * <p>
 * 水冷机组控制报文解析
 *
 * @author LiDaHai
 * @version 1.0.0
 * @since 2023/1/12
 */
public class DianlanWaterCoolingUnitControlDataGramReasonBits extends BitParserBase {

    public DianlanWaterCoolingUnitControlDataGram parseReason(String hexString) {
        String[]                               hexStrArray = hexStrToArray(hexString);
        DianlanWaterCoolingUnitControlDataGram info        = DianlanWaterCoolingUnitControlDataGram.builder().build();
        String                                 bitString   = HexUtil.hexToBinary(hexStrArray[0]);
        int                                    offset      = 0;
        info.setBmsThermalManagementRelayStatus(parseBmsThermalManagementRelayStatus(StrUtil.sub(bitString, offset, offset += 2)));
        info.setChargingStatus(parseChargingStatus(StrUtil.sub(bitString, offset, offset += 2)));
        info.setReserve(StrUtil.sub(bitString, offset, offset += 2) + "");
        info.setBmsRequestMode(parseBmsRequestMode(StrUtil.sub(bitString, offset, offset += 2)));
        info.setReserve1(hexStrArray[3] + hexStrArray[2] + hexStrArray[1]);
        info.setWaterTemperature(parseInt(hexStrArray[4]) + (-40));
        info.setReserve2(hexStrArray[5] + "");
        info.setBmsLifeFrame(parseInt(hexStrArray[6]) + "");
        info.setReserve3(hexStrArray[7] + "");
        return info;
    }

    private DianlanWaterCoolingUnitBmsRequestMode parseBmsRequestMode(String bits) {
        switch (bits) {
            case "00":
                return DianlanWaterCoolingUnitBmsRequestMode.OffMode;
            case "01":
                return DianlanWaterCoolingUnitBmsRequestMode.CoolingMode;
            case "10":
                return DianlanWaterCoolingUnitBmsRequestMode.HeatingMode;
            case "11":
                return DianlanWaterCoolingUnitBmsRequestMode.SelfCirculationMode;
            default:
                return DianlanWaterCoolingUnitBmsRequestMode.UNKNOWN;
        }
    }

    private DianlanWaterCoolingUnitChargingStatus parseChargingStatus(String bits) {
        switch (bits) {
            case "00":
                return DianlanWaterCoolingUnitChargingStatus.UnCharging;
            case "01":
                return DianlanWaterCoolingUnitChargingStatus.Charging;
            case "10":
            case "11":
                return DianlanWaterCoolingUnitChargingStatus.Invalid;
            default:
                return DianlanWaterCoolingUnitChargingStatus.UNKNOWN;
        }
    }

    private DianlanWaterCoolingUnitBmsThermalManagementRelayStatus parseBmsThermalManagementRelayStatus(String bits) {
        switch (bits) {
            case "00":
                return DianlanWaterCoolingUnitBmsThermalManagementRelayStatus.BreakOff;
            case "01":
                return DianlanWaterCoolingUnitBmsThermalManagementRelayStatus.Close;
            case "10":
            case "11":
                return DianlanWaterCoolingUnitBmsThermalManagementRelayStatus.Invalid;
            default:
                return DianlanWaterCoolingUnitBmsThermalManagementRelayStatus.UNKNOWN;
        }
    }
}